package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.entity.Semester;
import edu.zqu.instructionalMatters.project.mapper.SemesterMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 学期 - 前端控制器
 * @Version 1.0
 */

@RestController
@RequestMapping("/api")
@Api(tags = "学期")
public class SemesterController {
    @Autowired
    private SemesterMapper semesterMapper;

    @ApiOperation(value = "获取学期信息列表")
    @GetMapping(value = "/semesters")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "学期名称关键字", paramType = "query", required = false),
            @ApiImplicitParam(name = "englishName", value = "学期英文名关键字", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             String name,
                             String englishName
    ) {
        Semester semester = new Semester().setCollegeId(collegeId).setName(name).setEnglishName(englishName);           //合成对象
        List<Semester> list = semesterMapper.findAll(semester);                                                         //获取list
        return ZQUResult.success(list);                                                                                 //返回list
    }

    @ApiOperation(value = "获取学期信息", response = Semester.class)
    @GetMapping(value = "/semester/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学期id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        Semester semester = semesterMapper.selectById(id);
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", semester);
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/semester")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "学期名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "学期英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         String name,
                         String englishName,
                         BigInteger byWho) {
        Semester semester = new Semester();                                     //初始化
        semester.setCollegeId(collegeId);                                       //设置学校id
        semester.setName(name);                                                 //设置中文名称
        semester.setEnglishName(englishName);                                   //设置英文名称
        semester.setByWho(byWho);                                               //设置存在者
        semesterMapper.insert(semester);                                        //执行插入
        return ZQUResult.success();                                             //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/semester/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "name", value = "学期名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "学期英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            String name,
                            String englishName,
                            BigInteger byWho) {
        Semester semester = semesterMapper.selectById(id);                      //先查出旧纪录
        semester.setByWho(byWho);                                               //修改存在者
        semesterMapper.updateById(semester);                                    //更新操作者
        semesterMapper.deleteById(id);                                          //逻辑删除
        semester = new Semester();                                              //初始化新记录
        semester.setCollegeId(collegeId);                                       //设置学校id
        semester.setName(name);                                                 //设置中文名称
        semester.setEnglishName(englishName);                                   //设置英文名称
        semester.setByWho(byWho);                                               //设置存在者
        semester.setLastVersion(id);                                            //设置上一版本
        semesterMapper.insert(semester);                                        //执行插入
        return ZQUResult.success();                                             //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/semester/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        Semester semester = semesterMapper.selectById(id);                      //先查出旧纪录
        semester.setByWho(byWho);                                               //修改存在者
        semesterMapper.updateById(semester);                                    //更新操作者
        semesterMapper.deleteById(id);                                          //逻辑删除
        return ZQUResult.success();                                             //操作成功返回
    }
}

